#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
ll n,m,t,A[100005],a[100005],c[100005],b[100005],d[100005][30],l,winner,man[100005];
void dfs(ll y,ll len){
    if(len==1){
        winner=man[1];
        return ;
    }
    for(int i=1;i<=len;i+=2){
        if(d[y][(i+1)/2]==0){
            if(a[man[i]]>=y)b[(i+1)/2]=man[i];
            else b[(i+1)/2]=man[i+1];
        }
        else {
            if(a[man[i+1]]>=y)b[(i+1)/2]=man[i+1];
            else b[(i+1)/2]=man[i];
        }
    }
    for(int i=1;i<=len/2;i++)man[i]=b[i];
    dfs(y+1,len/2);
}
int main () {
    freopen("arena.in","r",stdin);
    freopen("arena.out","w",stdout);
    cin>>n>>m;
    for(int i=1;i<=n;i++)cin>>A[i];
    for(int i=1;i<=m;i++)cin>>c[i];
    string s;
    ll k=0;
    ll x=1;
    while(x<n){
        k++;
        x*=2;
    }
    l=x;
    for(int i=1;i<=k;i++){
        cin>>s;
        x/=2;
        for(int j=0;j<x;j++)d[i][j]=s[j-1]-'0';
        
    }
    cin>>t;
    while(t--){
        ll E,B,C,D;
        cin>>E>>B>>C>>D;
        for(int i=1;i<=n;i++){
            man[i]=i;
            if(i%4==0)a[i]=A[i]^E;
            else if(i%4==1)a[i]=A[i]^B;
            else if(i%4==2)a[i]=A[i]^C;
            else a[i]=A[i]^D;
        }
        ll ans=0;
        for(int i=1;i<=m;i++){
            for(int j=1;j<=n;j++)man[j]=j;
            dfs(1,c[i]);
            ans=ans^(i*winner);
        }
        cout<<ans<<endl;
    }
    return 0;
}